<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns="http://www.opml.org/2002/Attributes"
                xmlns:date="http://exslt.org/dates-and-times"
                xsl:extension-element-prefixes="date"
                version="1.0">
  <xsl:output method='xml' indent='yes'/>
  <xsl:strip-space elements="*" />

  <!-- Copyright (c) 2006 -->
  <!-- Author: King-On Yeung (koyeung@gmail.com) -->
  <!-- Homepage: http://koyeung.blogspot.com/ -->

  <!-- matches topmost element and output basic opml structures -->
  <xsl:template match="plist">
    <opml version="1.0">
      <head>
        <title>Safari Feeds</title>
        <dateCreated><xsl:value-of select="date:date-time()" /></dateCreated>
      </head>
      <body>
        <outline text="Safari RSS Feeds">
          <xsl:apply-templates select="dict/array"/>
        </outline>
      </body>
    </opml>
  </xsl:template>

  <!-- by default, print nothing -->
  <xsl:template match="dict" />

  <!-- match WebBookmarkTypeList node -->
  <xsl:template match="dict[key[text() = 'WebBookmarkType' and following-sibling::string[text() = 'WebBookmarkTypeList']]]">
    <outline>
      <xsl:attribute name="text">
        <xsl:value-of select="string[preceding-sibling::key[text() = 'Title']]" />
      </xsl:attribute>
      <xsl:apply-templates select="array" />
    </outline>
  </xsl:template>

  <!-- match WebBookmarkTypeLeaf node -->
  <xsl:template match="dict[key[text() = 'URIDictionary'] and starts-with(string[preceding-sibling::key[text() = 'URLString'] and text() ], 'feed')]">
    <outline type="rss">
      <xsl:attribute name="text">
        <xsl:value-of select="dict[preceding-sibling::key[text() = 'URIDictionary']]/string[preceding-sibling::key[text() = 'title']]"/>
      </xsl:attribute>
      <xsl:attribute name="description">
        <xsl:value-of select="dict[preceding-sibling::key[text() = 'URIDictionary']]/string[preceding-sibling::key[text() = 'title']]"/>
      </xsl:attribute>
      <xsl:attribute name="xmlUrl">
        <xsl:value-of select="concat('http',substring(string[preceding-sibling::key[text() = 'URLString']],5))" />
      </xsl:attribute>
      <xsl:attribute name="htmlUrl">
        <xsl:value-of select="concat('http',substring(string[preceding-sibling::key[text() = 'URLString']],5))" />
      </xsl:attribute>
    </outline>
  </xsl:template>

</xsl:stylesheet>
